package Interface;

import baperjakat.Koneksi;
import component.model.PenilaianPegawaiTabelModel;
import dao.PegawaiDao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import javax.swing.ImageIcon;
import model.Pegawai;

/**
 *
 * @author Marlon
 */
public class PenilaianPanel extends javax.swing.JPanel {
    private Connection c;
    private Statement s;
    private PenilaianPegawaiTabelModel model;
    private PegawaiDao pegawaiDao;
    
    public PenilaianPanel() throws SQLException {
        initComponents();
    
        btnCari.setIcon(new ImageIcon((getClass().getResource("/images/Finddata toolbar.png"))));
        btnCetak.setIcon(new ImageIcon((getClass().getResource("/images/print toolbar.png"))));
        btnSeleksi.setIcon(new ImageIcon((getClass().getResource("/images/Seleksi.png"))));
        btnSortir.setIcon(new ImageIcon((getClass().getResource("/images/Finddata toolbarS.png"))));
    
        tampilJabatan();
        pegawaiDao = new PegawaiDao();
        loadData();
    }

    private void loadData(){
        try {
            List<Pegawai> list = pegawaiDao.getAll();
            model = new PenilaianPegawaiTabelModel(list);
            tabelPegawai.setModel(model);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    
    private void tampilJabatan() throws SQLException{
        c = Koneksi.getKoneksi();
        s = c.createStatement();
        String sql = "select * from jabatan";
        ResultSet r = s.executeQuery(sql);
            
        cmbJabatan.addItem("= PILIH =");
        while(r.next()){
            cmbJabatan.addItem(r.getString("namajabatan"));
        }
        r.close();
        s.close();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cmbJabatan = new javax.swing.JComboBox();
        lblNip = new javax.swing.JLabel();
        txtNip = new javax.swing.JTextField();
        btnCari = new javax.swing.JButton();
        btnSeleksi = new javax.swing.JButton();
        btnSortir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelPegawai = new javax.swing.JTable();
        btnCetak = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(555, 600));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(":: Proses Penilaian Jabatan Struktural ::");

        jLabel2.setText("Jabatan :");

        lblNip.setText("Nama :");

        txtNip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNipActionPerformed(evt);
            }
        });

        btnCari.setText("Cari");
        btnCari.setPreferredSize(new java.awt.Dimension(65, 23));

        btnSeleksi.setText("Seleksi");
        btnSeleksi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleksiActionPerformed(evt);
            }
        });

        btnSortir.setText("Sortir");

        tabelPegawai.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelPegawai.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelPegawaiMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelPegawai);

        btnCetak.setText("Cetak");
        btnCetak.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnCetak.setOpaque(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(120, 120, 120))
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(lblNip))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cmbJabatan, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtNip, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(31, 31, 31)
                        .addComponent(btnCari, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(btnSeleksi)
                        .addGap(34, 34, 34)
                        .addComponent(btnSortir)
                        .addGap(39, 39, 39)
                        .addComponent(btnCetak)))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(cmbJabatan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNip)
                    .addComponent(txtNip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCari, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSeleksi)
                    .addComponent(btnSortir)
                    .addComponent(btnCetak))
                .addContainerGap(42, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtNipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNipActionPerformed
        try{
            List<Pegawai> list = pegawaiDao.cari(txtNip.getText());
            model = new PenilaianPegawaiTabelModel(list); 
            tabelPegawai.setModel(model);
        
        }catch (SQLException ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_txtNipActionPerformed

    private void btnSeleksiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleksiActionPerformed
        // TODO add your handling code here:
        FormSeleksiPegawai seleksi = new FormSeleksiPegawai(null, true);
        seleksi.setVisible(true);
    }//GEN-LAST:event_btnSeleksiActionPerformed

    private void tabelPegawaiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelPegawaiMouseClicked
        int i = tabelPegawai.getSelectedRow();
        if(i == -1)
            
            return;
        
        Pegawai pegawai = model.getPegawai(i);
//        setView(pegawai);

    }//GEN-LAST:event_tabelPegawaiMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCari;
    private javax.swing.JButton btnCetak;
    private javax.swing.JButton btnSeleksi;
    private javax.swing.JButton btnSortir;
    private javax.swing.JComboBox cmbJabatan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNip;
    private javax.swing.JTable tabelPegawai;
    private javax.swing.JTextField txtNip;
    // End of variables declaration//GEN-END:variables
}
